//const { timeStamp } = require("console")

// miniprogram/pages/my/my.js
var app = getApp()
var db=wx.cloud.database()
const util=require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') ,// 如需尝试获取用户信息可改为false
    userName:"请登录",
    logged:false,
    openID:"",
    nickName:""
    

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(app.globalData.userInfo&&app.globalData.openid){
      console.log(app.globalData.userInfo.nickName)
      console.log(app.globalData.userInfo)
      console.log(app.globalData.openid)
      console.log(app.globalData.test)
      //console.log(app.globalData.testopenid)
      this.setData({
        nickName:app.globalData.userInfo.nickName,
        avatarUrl:app.globalData.userInfo.avatarUrl,
        logged:true,
        openID:app.globalData.openid,
        userName:app.globalData.userInfo.nickName,
        nickName:app.globalData.userInfo.nickName
      })
      
      
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '我的',
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getUserProfile() {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res.userInfo)
        app.globalData.userInfo=res.userInfo
        
        wx.setStorageSync('userInfo', res.userInfo)
        this.setData({
          avatarUrl: res.userInfo.avatarUrl,
          //userInfo: res.userInfo,
          hasUserInfo: true,
          logged:true,
          userName:res.userInfo.nickName,
          nickName:res.userInfo.nickName
      
        })
        console.log(res.userInfo.nickName)
        console.log(app.globalData.userInfo.nickName)
        console.log(wx.getStorageSync('userInfo').nickName)
        this.AddNewUser()


      }
    })
  },
  userLogin: function(){
    if(!this.data.logged){
      this.getUserProfile();
      
    }
    
    
  },
  navigateTowfbd(){//navigateTowfbd
    if(this.data.logged){
    wx.navigateTo({
      url: 'myPublishing/myPublishing',
    })
  }
  else{
    wx.showToast({
      title: '请先登录',
    })
  }
  },
  publish:function(){
    wx.navigateTo({
      url: 'publish/publish',
    })

  },
  test:function(){
    app.globalData.test="123"
    console.log(app.globalData.test)
  },
  AddNewUser(){
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        //console.log('[云函数] [login] user openid: ', res.result.openid)
        //console.log(res)
        app.globalData.openid = res.result.openid
        wx.setStorageSync('openid', res.result.openid)
        //app.globalData.nickName=res.result.nickName
        this.setData({
          openID:res.result.openid,
          logged:true

        })
       db.collection('user_Info').where({
        _openid:this.data.openID
       }).get().then(res=>{
       // console.log(res)
         if(res.data == ''){
           db.collection('user_Info').add({
             data:{
              nickName:this.data.nickName,
              avatarUrl:app.globalData.userInfo.avatarUrl,
              openid:this.data.openID,
              time:util.formatTime(new Date())
             }
           }).then(res=>{
             console.log('添加成功')
           })
         }else{
         console.log('存在')
         }
       })
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
        
      }
    })

  }
})